<?php

/**
 * presentation actions.
 *
 * @package    beamr
 * @subpackage presentation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class presentationActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->beamr_presentation_list = $this->getUser()->getPresentations();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new BeamrPresentationForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new BeamrPresentationForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($beamr_presentation = $this->getUser()->getPresentation($request->getParameter('token')), 'This presentation does not exist.');
    $this->form = new BeamrPresentationForm($beamr_presentation);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($beamr_presentation = $this->getUser()->getPresentation($request->getParameter('token')), 'This presentation does not exist.');
    $this->form = new BeamrPresentationForm($beamr_presentation);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($beamr_presentation = $this->getUser()->getPresentation($request->getParameter('token')), 'This presentation does not exist.');
    $beamr_presentation->delete();

    $this->redirect('presentation/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $beamr_presentation = $form->save();

      $this->redirect('presentation/index');
    }
  }

  public function executeLatex(sfWebRequest $request)
  {
    $presentation = $this->getUser()->getPresentation($request->getParameter('token'));
    $this->forward404Unless($presentation, 'This presentation does not exist.');
    $this->latex = Beamr::getLatex($presentation);
    echo '<pre>'. $this->latex .'</pre>';
    return sfView::NONE;
  }

  public function executeDownload(sfWebRequest $request)
  {
    $presentation = $this->getUser()->getPresentation($request->getParameter('token'));

    if ($filename = Beamr::makeLatex($presentation)) {
      Beamr::makeThumbs($filename .'.pdf', $presentation);

      $this->getResponse()->setContentType('application/pdf');
      $this->getResponse()->addHttpMeta('cache-control', 'no-cache');
      $this->getResponse()->addHttpMeta('Expires', gmdate("D, d M Y H:i:s") . " GMT", time());
      $this->getResponse()->setContent(file_get_contents("$filename.pdf"));
      $this->getResponse()->addHttpMeta('Content-Disposition', 'attachment; filename="beamr.pdf"');

      Beamr::clean($filename);
    }
    return sfView::NONE;
  }
}
